﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;

namespace ParameterManagementSystem
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] argv)
        {
            if (argv.Count<string>() < 2)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                DataBase dataBase = new DataBase(argv[0]);
                XmlFile[] newArray;
                int count = 0;
                int countArgs = argv.Count<string>();
                if (File.Exists(argv[0]))
                {
                    XmlFile[] array = dataBase.LoadDataBaseContent();
                    newArray = new XmlFile[array.Count<XmlFile>() + argv.Count<string>() - 1];
                    array.CopyTo(newArray, 0);

                    count = array.Count<XmlFile>();
                }
                else
                {
                    newArray = new XmlFile[argv.Count<string>() - 1];
                }
                for (int i = 1; i < countArgs; i++)
                {
                    string fileName = argv[i];
                    int index = count + i - 1;
                    newArray[index] = new XmlFile();
                    newArray[index].GenerateID(newArray);
                    newArray[index].name = Path.GetFileName(fileName);
                    newArray[index].content = File.ReadAllText(fileName);
                    newArray[index].timeStamp = File.GetLastWriteTime(fileName);
                }
                dataBase = new DataBase(argv[0]);
                if (File.Exists(argv[0]))
                {
                    dataBase.DeleteAllData("XML_TABLE");
                }
                else {
                    dataBase.CreateNewDatabase();
                }
                dataBase.SaveFiles(newArray);
            }
        }
    }
}
